package com.intellagric.service.iftmonitor.Impl.ift_data_record;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.iftmonitor.DataRecordDao;
import com.intellagric.mapper.IftDataRecordMapper;
import com.intellagric.module.iftmonitor.DataRecordVo;
import com.intellagric.pojo.IftDataRecord;
import com.intellagric.pojo.IftDataRecordExample;
import com.intellagric.service.iftmonitor.ift_data_record.DataRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/21
 * @DESC： 数据记录服务实现
 */
@Service
public class DataRecordServiceImpl implements DataRecordService {

    @Autowired
    private IftDataRecordMapper dataRecordMapper;

    @Autowired
    private DataRecordDao dataRecordDao;

    /**
     * 添加数据记录
     * @param dataRecord
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addDataRecord(IftDataRecord dataRecord) {
        if (dataRecordMapper.insertSelective(dataRecord) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除数据记录
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteDataRecord(String id) {
        if (dataRecordMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改数据记录
     * @param dataRecord
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editDataRecord(IftDataRecord dataRecord) {
        if (dataRecordMapper.updateByPrimaryKeySelective(dataRecord) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询数据记录信息
     * @param id
     * @return IftDataRecord
     */
    @Override
    public IftDataRecord getDataRecordById(String id) {
        return dataRecordMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询数据记录信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getDataRecordList() {
        IftDataRecordExample dataRecordExample =  new IftDataRecordExample();
        dataRecordExample.createCriteria().andIdIsNotNull();
        List<IftDataRecord> dataRecordList = dataRecordMapper.selectByExample(dataRecordExample);
        return LayuiDataGridResult.success().add(dataRecordList,dataRecordList.size());
    }

    /**
     * 根据Vo查询数据记录信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getDataRecordListByVo(DataRecordVo dataRecordVo) {
        List<IftDataRecord> dataRecordList = dataRecordDao.selectDataRecordByVo(dataRecordVo);
        return LayuiDataGridResult.success().add(dataRecordList,dataRecordList.size());
    }

    /**
     * 分页查询数据记录信息列表
     * @param page
     * @param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getDataRecordPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        IftDataRecordExample dataRecordExample =  new IftDataRecordExample();
        dataRecordExample.setOrderByClause("acquisition_time DESC");
        dataRecordExample.createCriteria().andIdIsNotNull();
        List<IftDataRecord> dataRecordList = dataRecordMapper.selectByExample(dataRecordExample);
        return LayuiDataGridResult.success().add(dataRecordList,this.getDataRecordList().getCount());
    }

    /**
     * 分页关联查询数据记录信息列表
     * @param page
     * @param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getDataRecordPageAndRelation(int page, int rows) {
        PageHelper.startPage(page,rows);
        List<IftDataRecord> dataRecordList = dataRecordDao.selectDataRecordWithRelation();
        return LayuiDataGridResult.success().add(dataRecordList,this.getDataRecordList().getCount());
    }

    /**
     * 多条件分页关联查询数据记录信息列表
     * @param page
     * @param rows
     * @param dataRecordVo
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getDataRecordPageAndRelationByVo(int page, int rows,DataRecordVo dataRecordVo) {
        int count = dataRecordDao.selectDataRecordWithRelationByVo(dataRecordVo).size();
        PageHelper.startPage(page,rows);
        List<IftDataRecord> dataRecordList = dataRecordDao.selectDataRecordWithRelationByVo(dataRecordVo);
        return LayuiDataGridResult.success().add(dataRecordList,count);
    }
}
